package clientApp.gui;

import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JSeparator;
import javax.swing.ImageIcon;

import clientApp.textlogic.*;

import java.awt.Font;

public class EditTextPanel extends JTextArea {
	private JMenuItem undoButton;
	private JMenuItem redoButton;
	private JMenuItem cutButton;
	private JMenuItem copyButton;
	private JMenuItem pasteButton;
	private JMenuItem deleteButton;
	private JMenuItem selectAllButton;
	private JSeparator separator;
	private JSeparator separator_1;
	private TextTransfer textTransfer;
	
	public EditTextPanel() {
		setFont(new Font("Arial", Font.PLAIN, 12));
		
		setLineWrap(true);
		
		textTransfer = new TextTransfer();
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(this, popupMenu);
		
		undoButton = new JMenuItem("Відмінити");
		undoButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		undoButton.setEnabled(false);
		undoButton.setIcon(new ImageIcon(EditTextPanel.class.getResource("/resources/icons/arrow-curve-180-left.png")));
		popupMenu.add(undoButton);
		
		redoButton = new JMenuItem("Повернути");
		redoButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		redoButton.setEnabled(false);
		redoButton.setIcon(new ImageIcon(EditTextPanel.class.getResource("/resources/icons/arrow-curve.png")));
		popupMenu.add(redoButton);		
		
		separator = new JSeparator();
		popupMenu.add(separator);
		
		cutButton = new JMenuItem("Вирізати");
		cutButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		cutButton.setIcon(new ImageIcon(EditTextPanel.class.getResource("/resources/icons/scissors-blue.png")));
		cutButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String copyText = "";
				copyText = EditTextPanel.this.getSelectedText();
				EditTextPanel.this.replaceSelection("");
				textTransfer.setClipboardContents(copyText);
			}
		});
		popupMenu.add(cutButton);
		
		copyButton = new JMenuItem("Копіювати");
		copyButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		copyButton.setIcon(new ImageIcon(EditTextPanel.class.getResource("/resources/icons/document-copy.png")));
		copyButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String copyText = "";
				copyText = EditTextPanel.this.getSelectedText();
				textTransfer.setClipboardContents(copyText);
			}
		});
		popupMenu.add(copyButton);
		
		pasteButton = new JMenuItem("Вставити");
		pasteButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		pasteButton.setIcon(new ImageIcon(EditTextPanel.class.getResource("/resources/icons/clipboard-paste.png")));
		pasteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String pasteText = "";
				pasteText = textTransfer.getClipboardContents();
				EditTextPanel.this.append(pasteText);
			}
		});
		popupMenu.add(pasteButton);
		
		deleteButton = new JMenuItem("Видалити");
		deleteButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				EditTextPanel.this.replaceSelection("");
			}
		});
		popupMenu.add(deleteButton);
		
		separator_1 = new JSeparator();
		popupMenu.add(separator_1);
		
		selectAllButton = new JMenuItem("Виділити все");
		selectAllButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		selectAllButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				EditTextPanel.this.selectAll();
			}
		});
		popupMenu.add(selectAllButton);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	public JMenuItem getUndoButton() {
		return undoButton;
	}

	public JMenuItem getRedoButton() {
		return redoButton;
	}
	
	
}
